# bc
# tim Menzies
# Nov 18, 2012

function add_BC(lst,i,_BC) {
  bins1 = default(bins1,64)
  bins2 = default(bins2,8)
  min   = default(min,0)
  max   = default(max,100)
  update= default(update,10)
  for(j in lst)  
    add_BC1(lst[i],_BC)
  o(br2,"bins2")
}
function pid(x,i,br,cnt,i) {
  if      (n < br[1])  { k=1;    min[i] = x }
  else if (n > br[nrb]){ k= nrb; max[i] = x }
  else k = 2 + int((x - br[1]) / step)
  while (x < br[k - 1]) k--
  while (x > br[k])     k++
  cnt[k]++
  the["nr"]++ 
  if ((1+cnt[k])/(the["nr"]+2) > 1/nrb) {
    val = cnt[k]/2
    cnt[k] = val
    if (k==1) {
      for(j=the["nbr"+1;j>1;i--) {
	cnt[j] = cnt[j-1]
	br[j]  = br[j-1]
      }
      cnt[1] = cnt[2] = val
      br[1]  = n
    } else if (k==the["nrb"]) {
      br[ k+1] = x
      cnt[k+1] = cnt[k] = val
    } else {
      for(j=the["nbr"]+1;j>k;j--) {
	cnt[j] = cnt[j-1]
	br[ j] =  br[j-1]
      }
      cnt[k] = cnt[k+1] = val
      br[k] = n
    } 
    the["nrb"]++
  }}
  
